package com.dingreading.cloud.soms.service.impl;


import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.MstXcxStudentHomeworkDto;
import com.dingreading.cloud.soms.dto.MstXcxStudentLearningProgressDto;
import com.dingreading.cloud.soms.entity.Staff;
import com.dingreading.cloud.soms.entity.StaffRoles;
import com.dingreading.cloud.soms.service.*;
import com.dingreading.cloud.soms.util.DataProcessUtils;
import com.mybatisflex.core.paginate.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MstMstXcxAttendClassServiceImpl implements MstXcxAttendClassService {
    @Resource
    private DataProcessUtils dataProcessUtils;
    @Resource
    private MstXcxStudentLearningProgressService xcxStudentLearningProgressService;
    @Resource
    private MstXcxStudentHomeworkService xcxStudentHomeworkService;
    @Resource
    private StaffService staffService;
    @Resource
    private StaffRolesService staffRolesService;

    @Override
    public R<Object> pageListProgress(
            PageUtil pageUtil, String corpUid, String storeUid, String staffUid, String staffName,
            String keyword, Integer materialStage, String subjectUid, String beginDate, String endDate, String ip
    ) {
        if (StringUtils.isBlank(corpUid) || StringUtils.isBlank(storeUid) || StringUtils.isBlank(staffUid))
            return R.fail("伙伴字段缺少请检查！");


        Page<MstXcxStudentLearningProgressDto> page = xcxStudentLearningProgressService.pageList(
                pageUtil, corpUid, storeUid, staffUid, staffName, keyword, materialStage, subjectUid, beginDate, endDate
        );
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            dataProcessUtils.dealLearningProgress(page.getRecords(), ip);
        }
        PageDto<MstXcxStudentLearningProgressDto> pageDto = new PageDto<>();
        pageDto.setTotal(page.getTotalRow());
        pageDto.setList(page.getRecords());
        return R.ok(pageDto);
    }

    @Override
    public R<Object> pageListHomework(
            PageUtil pageUtil, String corpUid, String storeUid, String staffUid, String staffName,
            String keyword, String subjectUid, String beginDate, String endDate, String ip
    ) {
        if (StringUtils.isBlank(corpUid) || StringUtils.isBlank(storeUid) || StringUtils.isBlank(staffUid))
            return R.fail("伙伴字段缺少请检查！");

        Staff staff = staffService.getByStaffUid(corpUid, staffUid);
        if (staff == null)
            return R.fail("未查询到账号！");
        StaffRoles staffRole = staffRolesService.getStoreUidAndStaffUid(storeUid, staffUid);
        if (null == staffRole || StringUtils.isBlank(staffRole.getRoles()))
            return R.fail("员工角色权限获取失败");

        Page<MstXcxStudentHomeworkDto> page = xcxStudentHomeworkService.pageList(
                pageUtil, corpUid, storeUid, staffUid, staffName, keyword, subjectUid, beginDate, endDate, staff, staffRole
        );
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            dataProcessUtils.dealHomework(page.getRecords(), ip);
        }
        PageDto<MstXcxStudentHomeworkDto> pageDto = new PageDto<>();
        pageDto.setTotal(page.getTotalRow());
        pageDto.setList(page.getRecords());
        return R.ok(pageDto);
    }

    @Override
    public R<Object> getBean(String corpUid, String storeUid, String staffUid, String staffName, String uid, String ip) {
        if (StringUtils.isBlank(corpUid) || StringUtils.isBlank(storeUid) || StringUtils.isBlank(staffUid))
            return R.fail("伙伴字段缺少请检查！");
        if (StringUtils.isBlank(uid))
            return R.fail("作业uid不能为空！");

        return xcxStudentHomeworkService.getBean(corpUid, storeUid, staffUid, staffName, uid, ip);
    }

    @Override
    public R<Object> correct(
            String corpUid, String storeUid, String staffUid, String staffName, String uid,
            Integer status, Integer teacherScore, String evaluationText
    ) {
        if (StringUtils.isBlank(corpUid) || StringUtils.isBlank(storeUid) || StringUtils.isBlank(staffUid))
            return R.fail("伙伴字段缺少请检查！");
        if (StringUtils.isBlank(uid))
            return R.fail("作业uid不能为空！");
        if (status == null || status != 2 && status != -1)
            return R.fail("作业批改状态为空或者错误！");
        if (teacherScore == null)
            return R.fail("老师给学生评分不能为空！");
        if (status == -1 && StringUtils.isBlank(evaluationText))
            return R.fail("老师给学生评价不能为空！");

        Staff staff = staffService.getByStaffUid(corpUid, staffUid);
        if (staff == null)
            return R.fail("未查询到账号！");
        StaffRoles staffRole = staffRolesService.getStoreUidAndStaffUid(storeUid, staffUid);
        if (null == staffRole || StringUtils.isBlank(staffRole.getRoles()))
            return R.fail("员工角色权限获取失败");

        return xcxStudentHomeworkService.correct(
                corpUid, storeUid, staffUid, staffName, uid, status, teacherScore, evaluationText, staff, staffRole
        );
    }
}
